package cn.huaguo.infrastructure.persistent.repository;

import cn.huaguo.domain.order.model.entity.OrderEntity;
import cn.huaguo.domain.order.repository.IOrderRepository;
import cn.huaguo.infrastructure.persistent.dao.IOrderDao;
import cn.huaguo.infrastructure.persistent.po.Order;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description 订单仓储接口实现类
 * @Author wangQian
 * @Date 2024/5/13
 */
@Repository
public class OrderRepository implements IOrderRepository {
    @Resource
    private IOrderDao orderDao;

    /**
     * 新增订单仓储接口实现
     *
     * @param orderEntity
     */
    @Override
    public String addOrder(OrderEntity orderEntity) {
        Order order = new Order();
        BeanUtils.copyProperties(orderEntity, order);
        order.setOrderTime(LocalDateTime.now());
        orderDao.addOrder(order);
        return order.getOrderId();
    }

    /**
     * 根据创建订单人Id查询订单仓储接口实现
     *
     * @param createUserId
     * @return
     */
    @Override
    public List<OrderEntity> queryOrderInfoByCreateUserId(String createUserId) {
        List<Order> orders = orderDao.queryOrderInfoByCreateUserId(createUserId);
        List<OrderEntity> list = new ArrayList();
        for (Order order : orders) {
            OrderEntity orderEntity = OrderEntity.builder()
                    .orderId(order.getOrderId())
                    .createUserId(order.getCreateUserId())
                    .taskId(order.getTaskId())
                    .taskContent(order.getTaskContent())
                    .orderStatus(order.getOrderStatus())
                    .orderAmount(order.getOrderAmount())
                    .orderTime(order.getOrderTime())
                    .payUrl(order.getPayUrl())
                    .payTime(order.getPayTime())
                    .build();
            list.add(orderEntity);
        }
        return list;


    }

    /**
     * 订单完成仓储接口实现
     *
     * @param orderId
     * @param orderStatus
     * @return
     */
    @Override
    public Boolean updateOrderStatus(String orderId, String orderStatus) {
        return orderDao.updateOrderStatus(orderId, orderStatus);
    }

    /**
     * 根据订单id查询订单信息
     *
     * @param orderId
     * @return
     */
    @Override
    public OrderEntity queryOrderInfoByOrderId(String orderId) {
        Order order = orderDao.queryOrderInfoByOrderId(orderId);
        return OrderEntity.builder()
                .orderId(order.getOrderId())
                .createUserId(order.getCreateUserId())
                .taskId(order.getTaskId())
                .taskContent(order.getTaskContent())
                .orderStatus(order.getOrderStatus())
                .orderAmount(order.getOrderAmount())
                .orderTime(order.getOrderTime())
                .payUrl(order.getPayUrl())
                .payTime(order.getPayTime())
                .build();
    }
}
